﻿
var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            ID: 0,
            RoomID: 0,
            // ProjectName: '',
            ProjectID: '',
            ServiceFullName: '',//资源位置
            NameNumber:'',//资源编号
            AddCallPhone: '',//联系电话
            ServiceID:0,
            ServiceCardID:'',//服务卡ID
            ServiceCardName:'',//服务卡名字
            ServiceCardBalance:'',//服务卡余额
            Balance:'',//收款金额
            Explain:'',//说明
            AddCustomerName: '',//住户名称
            action: 'savamanualcharge',
        },
        IsRoomOpen:false,
        chooseID:0,
        cardList:[],

    },
    methods: {
        do_save: function () {
            var that = this;
            if (!that.form.ServiceFullName) {
                api.toast({
                    msg: '请选择资源位置',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.cardList.length<=0) {
                api.toast({
                    msg: '没有服务卡不能收款',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            
            if (!that.form.Balance) {
                api.toast({
                    msg: '请填写收款金额',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            toast.loading({
                title: "提交中",
                duration: 2000
            }, function (ret) { });
            var options = {};
            options.ID = that.form.ServiceID;
            options.Explain = that.form.Explain;
            options.Balance = that.form.Balance;
            options.action = "savamanualcharge";
            ns.post(options, function (succeed, data, err) {
                toast.hide();
                if (succeed) {
                    app.set_default();
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'home_new_updata',
                        extra: {
                            key1: 'value1',
                        }
                    });
                    setTimeout(function() {
                        api.closeWin();
                    }, 500)

                    

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },

        set_default: function () {
            api.getPrefs({
                key: 'username'
            }, function (ret, err) {
                if (ret.value) {
                    app.form.AddUserMan = ret.value;
                }
            });
            api.getPrefs({
                key: 'phonenumber'
            }, function (ret, err) {
                if (ret.value) {
                    app.form.PhoneNo = ret.value;
                }
            });
        },

        do_select_project: function () {
            var that = this;
            ns.openWin('chooseproject_frm', '选择项目');
        },
        get_select_project: function (data) {
            var that = this;
            if (data.projectid > 0) {
                that.form.RoomID = data.projectid;
            } else {
                that.form.RoomID = data.roomid;
            }
            that.form.ProjectID = data.projectid;
            that.form.ProjectName = data.name;
            that.form.NameNumber=data.namenum;
            that.form.ServiceFullName = data.name;
            that.getRoomOwner();
            that.getServiceCard();
        
        },
        getRoomOwner: function () {
            var that = this;
            if(!that.form.RoomID){
                return;
            }
            ns.get({ action: 'getroomrelationdata', ProjectID: that.form.RoomID }, function (succeed, data, err) {
                if (succeed) {
                    that.form.AddCustomerName = data.RealName;
                    that.form.AddCallPhone = data.PhoneNumber;
                }
            });
        },
        //获取服务卡
        getServiceCard:function(){
            var that = this;
            if(!that.form.RoomID){
                return;
            }
            
            ns.get({ action: 'getservicecardselection', RoomID:that.form.RoomID }, function (succeed, data, err) {
                if (succeed) {
                    that.cardList = data.data;
                    if(that.cardList.length<=0){
                        that.cardList=[];
                        that.form.ServiceID=0;
                        that.form.ServiceCardID = '';
                        that.form.ServiceCardName = '';
                        that.form.ServiceCardBalance = '';
                        api.toast({
                            msg: '该房间没有服务卡',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }else{

                        that.form.ServiceID=that.cardList[0].ID;
                        that.form.ServiceCardID = that.cardList[0].ServiceCardID;
                        that.form.ServiceCardName = that.cardList[0].ServiceCardName;
                        that.form.ServiceCardBalance = that.cardList[0].Balance;
                    }
                    
                    
                }
            });
        },
        do_choose_card:function(id){
            var that = this;
            if (!that.form.ServiceFullName) {
                api.toast({
                    msg: '请选择资源位置',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            that.IsRoomOpen=true;
        },
        doCloseRoom:function(){
            var that = this;
            that.IsRoomOpen=false;
        },
        doSelectcard:function(item){
            var that=this;
            that.form.ServiceID=item.ID;
            that.form.ServiceCardID=item.ServiceCardID;
            that.form.ServiceCardName=item.ServiceCardName;
            that.form.ServiceCardBalance=item.Balance;
            that.IsRoomOpen=false;
        },
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    // setTimeout(function () {
    //     app.form.AddCustomerName = api.getPrefs({
    //         sync: true,
    //         key: 'loginname'
    //     });
    // }, 500)
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');

    app.set_default();
    app.get_select_project(api.pageParam);
    api.addEventListener({
        name: 'do_choose_project_complete'
    }, function (ret) {
        app.get_select_project(ret.value);
    });



};
